package de.openkanban.client.datasource;

import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceImageField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.util.Page;

/**
 * Data Source for board Contacts. Requests data over HTTP via JSON.
 */
public class BoardContactDS extends DataSource {

	private static BoardContactDS instance;

	public static BoardContactDS getInstance(String parentBoardId) {
		if (instance == null) {
			instance = new BoardContactDS("boardContactDS", parentBoardId);
		}
		return instance;
	}

	private BoardContactDS(String id, String parentBoardId) {
		setID(id);
		DataSourceIntegerField userId = new DataSourceIntegerField("userId");
		userId.setPrimaryKey(true);

		DataSourceTextField namensKuerzel = new DataSourceTextField("namensKuerzel", "Vorname");

		DataSourceImageField avatar = new DataSourceImageField("avatarUrl", " ");
		avatar.setImageURLPrefix(Page.getAppImgDir() + "avatar/");
		avatar.setImageHeight(40);
		avatar.setImageWidth(40);

		setCacheAllData(false);
		setFields(avatar, namensKuerzel, userId);
		setDataFormat(DSDataFormat.JSON);
		setDataURL("service/board/" + parentBoardId + "/contact/get");
		setClientOnly(false);
	}

	/**
	 * @param dsRequest the DSRequest being processed
	 * @return the transformed request
	 */
	@Override
	protected Object transformRequest(DSRequest dsRequest) {
		return super.transformRequest(dsRequest);
	}

	public static void setInstanceNull() {
		BoardContactDS.instance = null;
	}

}
